<?php

/*
    Credentials class
    
        Author:     Alfredo Mungo
        License:    LGPL
        Copyright:  2012
*/

/*
    Class for storing credentials information
*/
class Credentials implements Serializable {

    private $user, $pass, $host;
    private $data;
    
    private static $storage; // Credentials storage
    
    /*
        Instantiates a new Credentials class.
        
        Arguments:
            string $user: The login username
            string $pass: The login password
            string $host: The login host
            mixed $data: Optional further login information
            
        Throws:
            WrongTypeException
    */
    function __construct($user, $pass = null, $host = null, $data = null) {
        if(is_string($user))
            $this->user = $user;
        else
            throw new WrongTypeException();
        
        if(is_string($pass) || $pass === null)
            $this->pass = $pass;
        else
            throw new WrongTypeException();
        
        if(is_string($host) || $host === null)
            $this->host = $host;
        else
            throw new WrongTypeException();
            
        $this->data = $data;
    }
    
    public function getUsername() { return $this->user; }    
    public function getPassword() { return $this->pass; }
    public function getHost() { return $this->host; }
    public function getData() { return $this->data; }
    
    public function checkUsername($user) { return $user === $this->user; }
    public function checkPassword($pass) { return $pass === $this->pass; }
    public function checkHost($host) { return $host === $this->host; }
    
    /*
        Throws:
            WrongTypeException
            
        NOTE: does not check against optional data
    */
    public function checkCredentials($cred) {
    
        if($cred instanceof Credentials)
            return $this->checkUsername($cred->user) && $this->checkPassword($cred->pass) && $this->checkHost($cred->host);
        else
            throw new WrongTypeException();
    }
    
    // Serializable implementation
    public function serialize() {
        $data = array($this->user, $this->pass, $this->host, $this->data);
        $serialized = serialize($data);
        
        return $serialized;
    }
    
    public function unserialize($serialized) {
        $data = unserialize($serialized);
        
        $this->user = $data[0];
        $this->pass = $data[1];
        $this->host = $data[2];
        $this->data = $data[3];
    }
    // Serializable implementation END
    
    
    // Static methods
    
    /*
        Register a Credentials instance into the global framework credentials pool.
        
        Throws:
            WrongTypeException
    */
    public static function register($name, $cred) {
    
        if($cred instanceof Credentials)
            self::$storage[$name] = $cred;
        else
            throw new WrongTypeException();
    }
    
    /*
        Unregister a Credentials instance into the global framework credentials pool.
    */
    public static function unregister($name) {
    
        if(isset(self::$storage[$name]))
            unset(self::$storage[$name]);
    }
    
    /*
        Get credentials from the global framework credentials storage.
    */
    public static function get($name) {
    
        if(isset(self::$storage[$name]))
            return self::$storage[$name];
        else
            return null;
    }
}

?>